const express = require('express')
const app = express()
const cors = require('cors');
const reader = require('./fs')
app.use(cors());
const port = 8085

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'Authorization,X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method')
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PATCH, PUT, DELETE')
    res.header('Allow', 'GET, POST, PATCH, OPTIONS, PUT, DELETE')
    next();
});
app.get('/getHomework', (req, res) => {
    console.log(req.url);
    let date = new Date()
    let m = date.getMonth()+1
    let d = date.getDate()
    let strDay = m + '-' + d
   
    reader.readdir(`../作业/${strDay}`, function(err, files) {
        if (!files) return res.status(200).send({
            code: 101,
            msg: '没有有效的作业'
        })
       files =  files.filter(item => {
           return item = item.replace(/[^\u4e00-\u9fa5]+/ig, '')
        })
        res.status(200).send({
            code: 200,
            arr: files,
            msg: '这些同学很棒'          
        })
    })

})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})